from app.model.ActivityModel import QyActivity,Activity_info
from django.views.decorators.csrf import csrf_exempt
from base.wechat_sdk.basic import WechatBasic
from app.model.MemberModel import QyMember
from base.myjson import JSONResponse
from base.Var import appid,appsecret
from django.db import transaction
from base.mybase64 import MyFile
from base.my_page import mypage
import json
import time


# 获取指定活动详情
@csrf_exempt
def Activity_list(request):
    if request.method == 'GET':
        try:
            obj = QyActivity.objects.filter(pk=request.GET.get('pk'))
        except:
            return JSONResponse(1007)
        # 进行分页
        pag = mypage(request.GET.get('page'), obj)
        serializer = Activity_info(pag['page_list'], many=True)
        return JSONResponse(0,serializer.data,pag['page_sum'])
    else:
        return JSONResponse(1054)


# 获取我组织的活动列表
@csrf_exempt
def Activity_org_list(request):
    if request.method == 'GET':
        try:
            originator_id = QyMember.objects.get(openid=request.GET.get('openid')).id
            obj = QyActivity.objects.filter(originator_id=originator_id)
        except:
            return JSONResponse(1007)
        # 进行分页
        pag = mypage(request.GET.get('page'), obj)
        serializer = Activity_info(pag['page_list'], many=True)
        return JSONResponse(0,serializer.data,pag['page_sum'])
    else:
        return JSONResponse(1054)


# 创建活动
@csrf_exempt
def Activity_create(request):
    if request.method == 'POST':
        try:
            data = json.loads(request.body)
            # 判断是否上传图片ID
            if data.get('media_id'):
                obj = WechatBasic(appid=appid, appsecret=appsecret)
                response = obj.download_media(data.get('media_id'))
                file, st = MyFile().Picture_download(response)
            else:
                st = 'static/avatar.png'
            obj = QyActivity(
                notice=data.get('notice'),
                activity_time=data.get('activity_time'),
                address_province=data.get('address_province'),
                address_city=data.get('address_city'),
                address_area=data.get('address_area'),
                address_detailed=data.get('address_detailed'),
                formater=data.get('formater', 0),
                type=data.get('type', 0),
                upper_limit=data.get('upper_limit'),
                lower_limit=data.get('lower_limit'),
                is_limit=data.get('is_limit', 0),
                limit_time=data.get('limit_time'),
                price=data.get('price'),
                activity_img=st,
                originator_id=data.get('originator_id'),
                add_time=str(time.time())
            )
            obj.save()
        except:
            return JSONResponse(1007)
        objs = QyActivity.objects.filter(pk=obj.pk)
        serializer = Activity_info(objs, many=True)
        return JSONResponse(0,serializer.data)
    else:
        return JSONResponse(1055)


# 修改指定活动
@csrf_exempt
def Activity_up(request):
    if request.method == 'PUT':
        data = json.loads(request.body)
        try:
            obj = QyActivity.objects.get(pk=data.get('pk'))
        except QyActivity.DoesNotExist:
            return JSONResponse(1007)
        try:
            with transaction.atomic():
                # 判断是否上传图片ID
                if data.get('media_id'):
                    img = WechatBasic(appid=appid, appsecret=appsecret)
                    response = img.download_media(data.get('media_id'))
                    file, st = MyFile().Picture_download(response)
                    obj.activity_img = st
                obj.notice = data.get('notice', obj.notice)
                obj.activity_time = data.get('activity_time', obj.activity_time)
                obj.address_province = data.get('address_province', obj.address_province)
                obj.address_city = data.get('address_city', obj.address_city)
                obj.address_area = data.get('address_area', obj.address_area)
                obj.address_detailed = data.get('address_detailed', obj.address_detailed)
                obj.formater = data.get('formater', obj.formater)
                obj.type = data.get('type', obj.type)
                obj.upper_limit = data.get('upper_limit', obj.upper_limit)
                obj.lower_limit = data.get('lower_limit', obj.lower_limit)
                obj.is_limit = data.get('is_limit', obj.is_limit)
                obj.limit_time = data.get('limit_time', obj.limit_time)
                obj.price = data.get('price', obj.price)
                obj.activity_state = data.get('activity_state', obj.activity_state)
                obj.save()
        except Exception as a:
            return JSONResponse(1000)
        objs = QyActivity.objects.filter(pk=obj.id)
        serializer = Activity_info(objs, many=True)
        return JSONResponse(0,serializer.data)
    else:
        return JSONResponse(1057)


# 删除指定活动
@csrf_exempt
def Activity_delete(request):
    if request.method == 'DELETE':
        data = json.loads(request.body)
        try:
            obj = QyActivity.objects.get(pk=data.get('pk'))
        except QyActivity.DoesNotExist:
            return JSONResponse(1007)
        obj.delete()
        return JSONResponse(0)
    else:
        return JSONResponse(1056)
